package neiltwist.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.JPanel;

public class Image extends JPanel {
   private BufferedImage image;
   private static Random randomGenerator = new Random();

   public Image() {
       image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);

       Graphics g = image.getGraphics();
       g.setColor(new Color(0x00000000));
       g.fillRect(0, 0, 512, 512);
       
       this.addMouseListener(new MouseListener(){
		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub	
		}
		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
       });
   }

   @Override
   protected void paintComponent(Graphics g) {
       g.drawImage(image, 0, 0, null);
   }

   public void process(int x, int y, int t) {
       Graphics g = image.getGraphics();

       double dx = ((double)x)/30.0;
       double dy = ((double)y)/30.0;
       double dz = ((double)t) /30.0;

//       double nval = SimplexNoise.turbulence(dx, dy, dz);
       
       double nval = randomGenerator.nextDouble();

       nval = nval < -1.0 ? -1.0 : (nval > 1.0 ? 1.0 : nval);

       nval = (nval + 1.0) / 2.0;

       int rval = (int) (nval * 255.0);
       int gval = (int) (nval * 255.0);
       int bval = (int) (nval * 255.0);

       int color = 0xff000000 | (rval << 16) | (gval << 8) | bval;

       image.setRGB(x, y, color);
   }
}
